<template>
  <view class="container">
    <view class="example">
      <uni-forms ref="form" :model="formData" labelWidth="80px">
        <uni-forms-item label="内部码" name="code" class="item-margin">
			<!--@confirm="submitForm"-->
          <uni-easyinput v-model="formData.code" placeholder="扫码" @blur="getWorkstation" focus  />
        </uni-forms-item>
		<uni-forms-item label="工位" name="workstationId" class="item-margin">
			<uni-data-select
				v-if="false"
				v-model="formData.workstationId"
				:localdata="workstationList"
			></uni-data-select>
			<uni-data-checkbox multiple mode="list" v-model="formData.workstationIdList" :localdata="workstationList"></uni-data-checkbox>
		</uni-forms-item>
        <!-- <uni-forms-item label="成品码" name="id" class="margin0">
		  <uni-easyinput v-model="formData.id" placeholder="成品码" ref="nextFocus" @confirm="submitForm" />
		</uni-forms-item> -->
      </uni-forms>
	  <view class="uni-form-item word-wrap">
	  	<view class=""> 扫描数据：{{formData.code}}</view>
	  </view>
    </view>
	<view class="footer-btn">
		<button type="primary" @click="submitForm">提交</button>
	</view>
  </view>
</template>

<script>
	import { clearWeightHistory, getWorkstationFromInternalCode } from "@/api/longyi/qualityManage/Index.js"
	
	export default {
	  data() {
	    return {
		  formData: {
			code: '',
			workstationIdList: undefined
		  },
		  workstationList: [],
		  loading: false,
	      rules: {
	        code: {
	          rules: [{
	            required: true,
	            errorMessage: '内部码不能为空'
	          }]
	        },
			workstationIdList: {
				rules: [{
				  required: true,
				  errorMessage: '工位不能为空'
				}]
			}
			
	      }
	    }
	  },
	  onReady() {
	    this.$refs.form.setRules(this.rules)
	  },
	  methods: {
		  submitForm() {
			  uni.showLoading()
			  
			  if(this.formData.workstationIdList == null || this.formData.workstationIdList.length == 0){
				  this.$modal.msgError("请选择要清理的工位!")
				  return;
			  }
			  
			  this.$refs.form.validate().then(res => {
				uni.hideLoading()
				
				this.$modal.confirm('确定清除该内码在此工位的采集数据吗？').then(() => {
				  clearWeightHistory(this.formData).then(response => {
					// console.log(response)
					if(response.data){
						//this.formData.code = ''
						//this.formData.workstationIdList = null
						this.getWorkstation()
						this.$modal.msgSuccess("清除成功！")
					}else{
						this.getWorkstation()
						this.$modal.msgSuccess("清除失败！")
					}
				  })
				})
				
			}).catch(err => {
				uni.hideLoading()
			})
			  
			  
			
		  },
		  getWorkstation(){
			  this.workstationList = [];
			  if(this.formData.code == null || this.formData.code == ''){
				  return;
			  }
			  
			  getWorkstationFromInternalCode(this.formData).then(response => {
				  if(response.data){
					  let workstationArray = [];
					  response.data.forEach(function(item){
						  workstationArray.push({'value':item.id, 'text':item.workstationName})
					  });
					  this.workstationList = workstationArray;
					  console.log("11:"+workstationArray)
				  }
				   
			  })
			  
		  }
	  }
	}
</script>

<style lang="scss">
  .example {
    padding: 15px;
    background-color: #fff;
	margin: 10px auto;
	.item-margin.is-direction-left {
	    margin-bottom: 15px !important;
	}
	.margin0.is-direction-left {
	    margin-bottom: 0 !important;
	}
  }
  .footer-btn{
	  padding: 15px;
	  background-color: #fff;
	  margin-top: 66px;
	  position: fixed;
	  width: 100%;
	  bottom: 0;
  }
  .container {
      //overflow: hidden;
  }
</style>